package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Key;
import java.util.*;

@Service
public class ItemCatServiceImpl implements ItemCatService{
    @Autowired
    private ItemCatMapper itemCatMapper;

    public Map<Integer,List<ItemCat>> getMap(){
        Map<Integer,List<ItemCat>> map = new HashMap<>();
        List<ItemCat> list = itemCatMapper.selectList(null);
        for (ItemCat itemCat:list){
            Integer key = itemCat.getParentId();
            if (map.containsKey(key)){
                map.get(key).add(itemCat);
            }else {
                ArrayList<ItemCat> childrenList = new ArrayList<>();
                childrenList.add(itemCat);
                map.put(key,childrenList);
            }
        }
        return map;
    }
    @Override
    public List<ItemCat> findItemCatList(Integer level) {
        Map<Integer, List<ItemCat>> map = getMap();
        if (level==1){
           return map.get(0);
        }
        if (level ==2){
            return getTwoList(map);
        }
        return getThreeList(map);
    }
    private List<ItemCat> getThreeList(Map<Integer, List<ItemCat>> map) {
        List<ItemCat> oneList = getTwoList(map);
        for (ItemCat oneItemCat : oneList){
            List<ItemCat> twoList = oneItemCat.getChildren();
            if (twoList ==null || twoList.size()==0){
                continue;
            }
            for (ItemCat twoItemCat:twoList){
                Integer key = twoItemCat.getId();
                List<ItemCat> threeList = map.get(key);
                twoItemCat.setChildren(threeList);
            }
        }
        return oneList;
    }

    private List<ItemCat> getTwoList(Map<Integer, List<ItemCat>> map) {
        List<ItemCat> oneList = map.get(0);
        for (ItemCat oneItemCat : oneList){
            Integer key = oneItemCat.getId();
            List<ItemCat> twoList = map.get(key);
            oneItemCat.setChildren(twoList);
        }
        return oneList;
    }

    /**
     * 思路:
     *      1.判断map集合中是否存在key
     *      2.如果key 不存在 准备一个新list集合,将自己作为第一个元素添加
     *      3.如果key 存 在  获取list集合,将自己追加.
     * @return
     */

    /*@Override
    public List<ItemCat> findItemCatList(Integer level) {
        long startTime = System.currentTimeMillis();
        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", 0);
        List<ItemCat> oneList = itemCatMapper.selectList(queryWrapper);
        for (ItemCat oneItemCat:oneList){
            Integer oneId = oneItemCat.getId();
            queryWrapper.clear();
            queryWrapper.eq("parent_id", oneId);
            List<ItemCat> twoList = itemCatMapper.selectList(queryWrapper);
            for (ItemCat twoItemCat:twoList){
                Integer twoId = twoItemCat.getId();
                queryWrapper.clear();
                queryWrapper.eq("parent_id", twoId);
                List<ItemCat> threeList = itemCatMapper.selectList(queryWrapper);
                twoItemCat.setChildren(threeList);
            }
            oneItemCat.setChildren(twoList);
            long endTime = System.currentTimeMillis();
            System.out.println(endTime-startTime);
        }
        return oneList;
    }*/
    @Override
    @Transactional //事务注解
    public void saveItemCat(ItemCat itemCat) {
        itemCat.setStatus(true);
        itemCatMapper.insert(itemCat);
    }

    @Override
    @Transactional
    public void deleteItemCat(ItemCat itemCat) {
        if (itemCat.getLevel()==3){
            itemCatMapper.deleteById(itemCat.getId());
            return;
        }
        if (itemCat.getLevel()==2){
            Integer twoId = itemCat.getId();
            QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id", twoId);
            itemCatMapper.delete(queryWrapper);
            itemCatMapper.deleteById(twoId);
            return;
        }
        int oneId = itemCat.getId();
        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", oneId);
        List twoIdList = itemCatMapper.selectObjs(queryWrapper);
        if (twoIdList.size()==0){
            itemCatMapper.deleteById(oneId);
        }else {
            queryWrapper.clear();
            queryWrapper.in("parent_id",twoIdList).or().in("id",twoIdList).or().eq("id", oneId);
            itemCatMapper.delete(queryWrapper);
        }
    }

    @Override
    @Transactional
    public void updateStatus(ItemCat itemCat) {
        itemCat.setUpdated(new Date());
        itemCatMapper.updateById(itemCat);
    }

    @Override
    @Transactional
    public void updateItemCat(ItemCat itemCat) {
        itemCatMapper.updateById(itemCat);
    }

}
